package com.ceadeal.admin.web.user;

import com.ceadeal.admin.common.api.ApiResult;
import com.ceadeal.admin.entity.User;
import com.ceadeal.admin.repository.UserRepository;
import com.ceadeal.admin.service.user.UserService;
import com.ceadeal.admin.util.OAuth2Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 功能描述: 用户接口
 *
 * 作者: Yzw
 * 日期: 2017-03-24 13:37:44
 */
@RestController
@RequestMapping("/api/user")
public class ApiUserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserRepository userRepository;


    @PostMapping("/register")
    public ApiResult register() {
        // TODO:

        return ApiResult.success();
    }

    /**
     * 功能描述: 用户修改
     * @param
     * @return
     * @throws
     *
     * @author Yzw
     * @date 2017-03-24 13:38:57
     */
    @PutMapping
    public ApiResult save() {
        // 用户认证
        if (!OAuth2Util.isAuthenticated()) { return ApiResult.loginError(); }
        Long userId = OAuth2Util.getUser().getId();

        // TODO:

        return ApiResult.success();
    }

    /**
     * 功能描述: 获取用户信息
     * @param
     * @return
     * @throws
     *
     * @author Yzw
     * @date 2017-03-24 14:05:36
     */
    @GetMapping("/{id}")
    public ApiResult get(@PathVariable("id") Long id) {
//        User user = this.userService.find(id);
//        User user = this.userRepository.findOne(id);
        User user = this.userRepository.getUserInfo(id);
        return ApiResult.success(user);
    }

    /**
     * 功能描述: 获取用户信息列表
     * @param
     * @return 
     * @throws 
     * 
     * @author Yzw
     * @date 2017-03-24 14:05:43
     */
    @GetMapping
    public ApiResult list() {
        List<User> userList = this.userService.findAll();
        return ApiResult.success(userList);
    }

    /**
     * 功能描述: 删除用户
     * @param
     * @return
     * @throws
     *
     * @author Yzw
     * @date 2017-03-24 14:10:16
     */
    @DeleteMapping("/{id}")
    public ApiResult del() {
        // TODO:
        return ApiResult.success();
    }

}
